<?php
	/* favourites table script */
	
	//start the session
	session_start();
	
	//checks if user has logged in
	if(!isset($_SESSION['username']))
	{
		$_SESSION['message'] = "<span style='color: red; font-weight: bold;'>You are not logged in to view this page.</span>";
		header('Location: Login.php');
	}
	
	//checks that the form has been submitted
 	if(isset($_POST['submit']))
	{	
		//gives a variable name for the POST variables
		$category_sort = $_POST['category_sort'];
		$sort_by = $_POST['sort_by'];
	
		//validates that if a category or sort is not selected the user gets an error message stating
		//that one or the other has not been selected
		if($category_sort == 'Category:')
		{
			$_SESSION['message'] = "<span style='color: red; font-weight: bold;'>A Category has not been selected.</span>";
			header('Location: sort_page.php');
		}
		elseif($sort_by == 'Sort by:')
		{
			$_SESSION['message'] = "<span style='color: red; font-weight: bold;'>A sorting method has not been selected</span>";
			header('Location: sort_page.php');
		}
	}
	else
	{
		//if the page is accessed outside the sort page, these are the default settings the view the table
		$category_sort = 'All';
		$sort_by = 'No Sort';
	}
	
	//validates if category all has been selected or not, if yes sets show_all to true if not show_all stays false
	if($category_sort == 'All')
	{
		$show_all = true;
	}
	else
	{
		$show_all = false;
	}
	
	//retrieves the file into an array where each line is a value
	$bookmarks_file = './bookmarks_data/bookmark_database.txt';
	$favourites = file($bookmarks_file);
	
	//explodes the text file into an array indexing all the items
	for($i = 0; $i < count($favourites); $i++)
	{
		$temp = explode(' | ', $favourites[$i]);
		$favourite[$i] = array('category' => $temp[0], 'url' => $temp[1], 'name' => $temp[2], 'date' => $temp[3], 'counter' => $temp[4]);
	}
	
	//loops through each particular element in the array until the array ends for sorting purposes
	foreach($favourite as $key => $value)
	{
		$category[$key] = $value['category'];
		$url[$key] = $value['url'];
		$name[$key] = $value['name'];
		$date[$key] = $value['date'];
		$counter[$key] = $value['counter'];
	}
	//validates the sort by drop down list and sorts them into particular category
	if($sort_by == 'Category')
	{
		//sorts category assending
		array_multisort($category, SORT_ASC, $favourite);
	}
	elseif($sort_by == 'Name')
	{
		//sorts by name assending
		array_multisort($name, SORT_ASC, $favourite);
	}
	elseif($sort_by == 'Date')
	{
		//sorts by date desending
		array_multisort($date, SORT_DESC, $favourite);
	}
	elseif($sort_by == 'Counter')
	{
		//sorts counter assending
		array_multisort($counter, SORT_DESC, $favourite);
	}
?>

<!-- favourites_table.php view -->

<html>
	<head>
		<title>Favourite Table</title>
		<link rel="stylesheet" type="text/css" href="css/style.css" />
	</head>
	
	<body>
		<div id="wrapper">
			<div id="banner"><img src="images/bannerv0.jpg" alt="header graphic"></div>
			<div id="wrapper_links"><?php include("menu/menu.php");?></div>	
			<div id="wrapper_body">

				<p align="left">User: <?php echo $_SESSION['username'];?></p>

				<h3>Favourites List</h3>
   						
				<center>
					<table border="1" bgcolor="#202020">
						<tr>
							<th>Category</th>
							<th>Name</th>
							<th>Date</th>
							<th>Counter</th>
							<th>Delete</th>
						</tr>
						
						<?php 
							//creates a foreach loop that cycles through the $favourite array 
							//outputting its variables into a table same as the foreach in 
							//the else if block
							foreach($favourite as $key => $row)
							{
								//this if statment validates the selection of a category
								if($show_all)
								{
						?>
						
						<tr>
							<td><?php echo $row['category']?></td>
							<td><a href="processes/count.php?url=<?php echo $row['url'];?>"><?php echo $row['name'];?></a></td>
							<td><?php echo date('Y/m/d', $row['date']);?></td>
							<td><?php echo $row['counter'];?></td>
							<td><a href="processes/delete_favourite.php?url=<?php echo $row['url'];?>&category=<?php echo $row['category'];?>">Delete</a></td>
						</tr>
						
						
						<?php 
								}
								elseif($row['category'] == $category_sort && !$show_all)
								{
						?>
						
						<tr>
							<td><?php echo $row['category']?></td>
							<td><a href="processes/count.php?url=<?php echo $row['url'];?>"><?php echo $row['name'];?></a></td>
							<td><?php echo date('Y/m/d', $row['date']);?></td>
							<td><?php echo $row['counter'];?></td>
							<td><a href="processes/delete_favourite.php?url=<?php echo $row['url'];?>&category=<?php echo $row['category'];?>">Delete</a></td>
						</tr>
						
						<?php 
								}
							}
						?>
						
					</table>
				</center>

				<?php
					if(isset($_SESSION['message']))
					{
						//outputs the error/confirmation message from the record_bookmarks.php script on this page
						echo $_SESSION['message'] . '<br />';
					}
				?>
					
			</div>
		</div>
	</body>
</html>